#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022-3-2 11:11
# @Author : huabao.tan
# @File : api.py
# @Software: PyCharm

import requests
import re
import time


class Daily:

    def __init__(self):
        self.response = Daily.get_request(self)
        self.data = Daily.data(self)
        self.id = Daily.id(self)

    def url(self):
        return "https://www.jx3api.com/app/daily"

    def params(self):
        return '{"server":"长安城"}'

    def get_request(self):
        res = requests.get(url=Daily.url(self), params=Daily.params(self))
        return res.json()

    def id(self):
        res = self.response
        res = res['data']['war']
        return res

    def data(self):
        res = self.response
        res = res['data']['date']
        return res

    def week(self):
        res = self.response
        res = res['data']['week']
        return res

    def week_team(self):
        res = self.response
        res = res['data']['team']
        res = str(res[2])
        res = res.replace(';', '  ')
        return '[%s]' % res

    def daily(self):
        print('时间：%s' % self.data + '\n' + str('星期：星期%s' % Daily.week(self)) + '\n' + '秘境日常：%s' % self.id
              + '\n' + '周常副本：%s' % Daily.week_team(self))


class Discourse:

    def __init__(self):
        pass

    def get_request(self):
        url = 'https://www.jx3api.com/app/random'
        res = requests.get(url)
        return res.json()

    def discourse(self):
        res = Discourse.get_request(self)
        res = res['data']['text']
        return res


class Price:

    def __init__(self, name):
        self.name = name
        self.goods_json = Price.get_requset(self)

    def url(self):
        return 'https://www.jx3api.com/app/price?name=%s' % self.name

    def get_requset(self):
        try:
            response = requests.get(url=Price.url(self))
            return response.json()
        except Exception as e:
            print(e)
            return '输入的关键词错误'

    def info(self):
        try:
            data = self.goods_json
            print(data['data']['info'])
            print('物品图片链接：%s' % data['data']['upload'])
            data = data['data']['data']
            for b in data:
                for a in b:
                    print('物品名字：%s  ' % a['name'], '类型：%s  ' % a['type'], '服务器：%s' % self.dealStr(a['server']),
                          'Zone：%s' % self.dealStr(a['zone']),
                          '价格：%s ' % self.dealStrPrice(str(a['price']) + 'RMB', 0), '类型：%s ' % a['sales'],
                          '时间：%s  ' % a['time'])
        except Exception as e:
            print(e)
            return e

    def dealStr(self, str, type=None):
        size = len(str)
        a = 6 - size
        if type == None:
            str += "　" * a
        else:
            str += " " * a
        return str

    def dealStrPrice(self, str, type=None):
        size = len(str)
        a = 9 - size
        if type == None:
            str += "　" * a
        else:
            str += " " * a
        return str


def query_info():
    inputs = input('输入要查询的物品信息：')
    print('--' * 20)
    p = Price('%s' % inputs)
    p.info()


def daily():
    print('--' * 30)
    d = Daily()
    d.daily()


def dis():
    print('--' * 30)
    d = Discourse()
    print(d.discourse())


class Announce:

    def url(self):
        return 'https://www.jx3api.com/app/announce'

    def params(self):
        return '{"limit": 5}'

    def res(self):
        return requests.get(url=self.url(), params=self.params()).json()

    def nwe_announce(self):
        res = self.res()['data']
        for a in res:
            title = a['title']
            date = a['date']
            url = a['url']
            return '标题：%s' % title, '时间：%s' % date, '相关链接：%s' % url

    def reg(self):
        result = str(self.nwe_announce())
        reg = "[(),'']"
        result = re.sub(reg, '', result)
        result = result.replace(' ', '\n')
        return result


class Serendipity:

    def __init__(self, server, name):
        self.server = server
        self.name = name

    def url(self):
        return 'https://www.jx3api.com/app/serendipity'

    def params(self):
        return {"server": "%s" % self.server, "name": "%s" % self.name, "serendipity": ""}

    def res(self):
        try:
            result = requests.get(url=self.url(), params=self.params()).json()
            # print(result)
            return result
        except Exception as e:
            return e

    def main(self):

        try:

            res = self.res()['data']
            # print(res)
            if res is None:
                print('<!--这是个黑鬼,奇遇都没有!-->')
            elif len(res) == 0:
                print('<!--这是个黑鬼,奇遇都没有!-->')

            for a in res:
                print(a)

                now_time = time.localtime(a['time'])
                dt = time.strftime("%Y-%m-%d %H:%M:%S", now_time)
                result = []
                result += '服务器：%s' % a['server'], 'ID：%s' % a['name'], '奇遇：%s' % a['serendipity'], '时间：%s' % str(dt)

                reg = "[(),'']"
                result = re.sub(reg, '', str(result))
                print(result)
        except Exception as e:
            print(e)
            return e


class Matrix:

    def __init__(self, name):
        self.name = name

    def url(self):
        return 'https://www.jx3api.com/app/matrix'

    def params(self):
        return {'name': '%s' % self.name}

    def response(self):
        return requests.get(url=self.url(), params=self.params()).json()

    def main(self):

        res = self.response()['data']
        if res == None:
            print('<!--输入的参数错误--!>')
        elif len(res) == 0:
            print('<!--输入的参数错误--!>')
        try:
            print('角色：%s' % res['name'], '阵眼名称：%s' % res['skillName'])
            for a in res['descs']:
                print('%s:%s' % (a['name'], a['desc']))
        except:
            pass


class New:

    def url(self):
        url = 'https://www.jx3api.com/app/news'
        return url

    def response(self):
        result = requests.get(url=self.url()).json()
        return result

    def main(self):
        try:
            result = self.response()
            if result == None:
                print('没有拿到数据!!')
            elif len(result) == 0:
                print('没有拿到数据！')

            data = result['data']
            for a in data:
                title = a['title']
                date = str(a['date'])
                date1 = date.replace('/', '月')
                url = a['url']
                print('标题：' + title + '\n' + '时间：' + date1 + '\n' + '相关链接：' + url)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    daily()  # 日常
    # dis()  # 骚话
    # query_info()  #物价查询并写入数据库
    a = Announce()
    print(a.reg())  # 更新公告
    s = Serendipity('长安城', '游轩')
    s.main()
    m = Matrix('冰心诀')
    m.main()
    # n = New()
    # n.main()
